﻿using LotusIslandOrderProcessing.model;
using LotusIslandOrderProcessing.PLCMake;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;
using static LotusIslandOrderProcessing.model.EWMMsg;
using static LotusIslandOrderProcessing.model.Save;

namespace LotusIslandOrderProcessing
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 咖啡机
        /// </summary>
        public static List<CoffeeMakeModel> makeModels = new List<CoffeeMakeModel>();
        /// <summary>
        /// 奶茶
        /// </summary>
        public static List<CoffeeMakeModel> makeModel1s = new List<CoffeeMakeModel>();
        /// <summary>
        /// 冰淇淋
        /// </summary>
        public static List<CoffeeMakeModel> makeModel2s = new List<CoffeeMakeModel>();
        public static string IPorPortUrl = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"IPorPortMessage.txt";
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!File.Exists(IPorPortUrl))
                {
                    using (FileStream stream = File.Create(IPorPortUrl))
                    {
                        stream.Close();
                        stream.Dispose();
                        IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
                        File.WriteAllText(IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
                    }
                }
                else
                {
                    var dd = File.ReadAllText(IPorPortUrl);
                    var porPortInfo = JsonConvert.DeserializeObject<IPorPortInfo>(File.ReadAllText(IPorPortUrl));
                    IPorPortInfo.Instance.CoffeeIP = porPortInfo.CoffeeIP;
                    IPorPortInfo.Instance.CoffeePort = porPortInfo.CoffeePort;
                    IPorPortInfo.Instance.RobotIPLeft = porPortInfo.RobotIPLeft;
                    IPorPortInfo.Instance.RobotIPRight = porPortInfo.RobotIPRight;
                    IPorPortInfo.Instance.PortName = porPortInfo.PortName;
                    IPorPortInfo.Instance.PortNameR = porPortInfo.PortNameR;
                    IPorPortInfo.Instance.PrinterCoffeeIP = porPortInfo.PrinterCoffeeIP;
                    IPorPortInfo.Instance.PrinterCofferPort = porPortInfo.PrinterCofferPort;
                    IPorPortInfo.Instance.EWMPortName = porPortInfo.EWMPortName;
                    IPorPortInfo.Instance.ZBPortName = porPortInfo.ZBPortName;
                    IPorPortInfo.Instance.CoffeePortName = porPortInfo.CoffeePortName;
                    IPorPortInfo.Instance.SugarInit1 = porPortInfo.SugarInit1;
                    IPorPortInfo.Instance.SugarInit2 = porPortInfo.SugarInit2;
                    IPorPortInfo.Instance.SugarInit3 = porPortInfo.SugarInit3;
                }
                //EWM eWM = new EWM();
                //eWM.orderNum = "2025062597494855";
                //eWM.deviceId ="22";
                //string list = JsonConvert.SerializeObject(eWM);
                //var data = PLCMake.HttpMsg.Instance.orderQueueMsg(list);
                TestDelegateInvoke testDelegate = new TestDelegateInvoke(DelegateInvokeMethod);
                testDelegate.BeginInvoke(null, null);
                PlcMakes.Instance.Init();
                Thread thr = new Thread(PlcMakes.Instance.SelRead);
                thr.IsBackground = true;
                thr.Start();

                Thread thr1 = new Thread(MessageShow);
                thr1.IsBackground = true;
                thr1.Start();
                Thread thr2 = new Thread(EWMMsg.Instance.IOSerial_Thr);
                thr2.IsBackground = true;
                thr2.Start();
                Thread thr3 = new Thread(OrderRequestDeleteOrOut.Instance.DYShow);
                thr3.IsBackground = true;
                thr3.Start();
                Thread thr4 = new Thread(OrderRequestDeleteOrOut.Instance.CCShow);
                thr4.IsBackground = true;
                thr4.Start();
                Thread thr5 = new Thread(OrderRequestDeleteOrOut.Instance.OrderDelete);
                thr5.IsBackground = true;
                thr5.Start();
                Thread thr6 = new Thread(DYCX1);
                thr6.IsBackground = true;
                thr6.Start();
                //Thread thr7 = new Thread(DYCX2);
                //thr7.IsBackground = true;
                //thr7.Start();
            }
            catch (Exception)
            {
                return;
            }
        }
        public delegate void TestDelegateInvoke();
        private void DelegateInvokeMethod()
        {
            try
            {
                saveDeviceMatter save = new saveDeviceMatter();
                save.matterStatusJson = JsonConvert.SerializeObject(CoffeeMatters.Instance);
                save.deviceStatusJson = JsonConvert.SerializeObject(deviceStatus.Instance);
                var saves = JsonConvert.SerializeObject(save);
                model.HttpMsg.Instance.saveDeviceMatterMsg(saves);

            }
            catch (Exception)
            {
                return;
            }
        }
        public void DYCX1()
        {
            while (true)
            {
                try
                {
                    PrinterWebSocket.Instance.DyCX1();
                    Thread.Sleep(1000);
                    if (CoffeeMatters.Instance.IslandMatter54 == 1)
                    {
                        TestDelegateInvoke testDelegate = new TestDelegateInvoke(DelegateInvokeMethod);
                        testDelegate.BeginInvoke(null, null);
                        break;
                    }
                }
                catch (Exception)
                {
                    Thread.Sleep(1000);
                }

            }
        }
      
        /// <summary>
        /// 订单查询
        /// </summary>
        public void MessageShow()
        {
            while (true)
            {
                try
                {
                    Device device = new Device();
                    string lists = JsonConvert.SerializeObject(device);
                    var data = model.HttpMsg.Instance.deviceOrderQueueListMsg(lists);
                    if (data != null)
                    {
                        var msglist = JsonConvert.DeserializeObject<HTTPMsg>(data);
                        if (msglist != null)
                        {
                            if (msglist.data.ToString().Length > 10)
                            {
                                var list = JsonConvert.DeserializeObject<List<deviceOrderQueueList>>(msglist.data.ToString());
                                if (list.Count > 0)
                                {
                                    if (list.Count > 0)
                                    {
                                        var ddd2 = list.FirstOrDefault(a => a.typeList1.Any(c => (c.status == "3" || c.status == "4") && !makeModel1s.Any(b => c.orderId == b.OrderId && c.id == b.OrderGoodsId)));
                                        if (ddd2 != null)
                                        {
                                            if (ddd2.typeList1.Count > 0)
                                            {
                                                var make = ddd2.typeList1.FirstOrDefault(a => (a.status == "3" || a.status == "4") && !makeModel1s.Any(b => a.orderId == b.OrderId && a.id == b.OrderGoodsId));
                                                if (make != null)
                                                {
                                                    if (makeModel1s.Count < 2)
                                                    {
                                                        if (!makeModel1s.Any(a => a.OrderMaking == 1) || makeModel1s.Any(a => a.OrderMaking == 2))
                                                        {
                                                            MilkMaking(make, ddd2.orderNum);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    Thread.Sleep(1000);
                }
                catch (Exception)
                {
                    Thread.Sleep(1000);
                }
            }
        }
        public static void MilkMaking(DeviceOrderQueue list, string orderNum, bool CoffeeBol = true)
        {
            try
            {

                var msgs = JsonConvert.DeserializeObject<List<specification>>(list.jsonCodeVal);
                Console.WriteLine(DateTime.Now.ToString() + "  cx10" + list.jsonCodeVal);
                int sugarCode = 0;//果糖
                string classCode = "";//品类
                int iceCode = 0;//冰量
                                // int cupCode = 0;//杯型
                if (msgs != null)
                {
                    if (msgs.Any(a1 => a1.SugarCode != ""))
                    {
                        sugarCode = msgs.Any(a1 => a1.SugarCode == "1") ? 1 : msgs.Any(a1 => a1.SugarCode == "2") ? 2 : msgs.Any(a1 => a1.SugarCode == "3") ? 3 : 0;
                    }
                    if (msgs.Any(b => b.classCode != ""))
                    {
                        classCode = msgs.FirstOrDefault(b => b.classCode != "").classCode;
                    }
                    if (msgs.Any(b => b.IceMassCode != ""))
                    {
                        iceCode = msgs.Any(b => b.IceMassCode != "1") ? 1 : msgs.Any(b => b.IceMassCode != "2") ? 2 : msgs.Any(b => b.IceMassCode != "3") ? 3 : 0;
                    }
                }

                int classcode = int.TryParse(classCode, out int aa) ? aa : 0;
                int OrderNumID = !makeModel1s.Any(a => a.OrderMaking == 1) ? 1 : !makeModel1s.Any(a => a.OrderMaking == 2) ? 2 : 0;//订单几口
                Console.WriteLine("zuopin");
                if (classcode != 0 && OrderNumID != 0)
                {
                    while (true)
                    {
                        try
                        {

                            EditDeviceOrderStatus editDeviceOrder1 = new EditDeviceOrderStatus();
                            editDeviceOrder1.status = 4;
                            editDeviceOrder1.orderId = list.orderId;
                            editDeviceOrder1.orderGoodsId = list.id;
                            string lit = JsonConvert.SerializeObject(editDeviceOrder1);
                            var ddd1 = model.HttpMsg.Instance.editDeviceOrderStatusMsg(lit);
                            if (ddd1 != null)
                            {
                                if (ddd1 != "")
                                {
                                    var list10 = JsonConvert.DeserializeObject<ListMsg>(ddd1);
                                    if (list10 != null)
                                    {
                                        if (list10.code == "0" || list10.code == "1")
                                        {
                                            Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                                            break;
                                        }
                                    }
                                    Console.WriteLine(DateTime.Now.ToString() + "  cx25 " + ddd1);
                                }
                            }
                        }
                        catch (Exception)
                        {
                        }
                        Thread.Sleep(100);
                    }
                    string guidnew = Guid.NewGuid().ToString();
                    int classMid = classcode / 10;
                    int classMak = classcode % 10;
                    PlcMakes.Instance.readBol = false;
                    Thread.Sleep(2000);
                    if (OrderNumID == 1)
                        OrderRequst.Instance.OrderMilkTea1(classMak, iceCode, sugarCode, 0, classMid == 781 ? 1 : 0, guidnew);
                    else
                        OrderRequst.Instance.OrderMilkTea2(classMak, iceCode, sugarCode, 0, classMid == 781 ? 1 : 0, guidnew);
                    Thread.Sleep(2000);
                    PlcMakes.Instance.readBol = true;
                    CoffeeMakeModel makeModel = new CoffeeMakeModel();
                    makeModel.Guid = guidnew;
                    makeModel.OrderMaking = OrderNumID;
                    makeModel.OrderGoodsId = list.id;
                    makeModel.OrderId = list.orderId;
                    makeModel.OrderNum = orderNum;
                    makeModel.LHImg = list.lhImgPath;
                    makeModel.Type = 0;
                    makeModel1s.Add(makeModel);
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
              //  PrinterWebSocket.Instance.DyCS2(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"打印\打印图片1.jpg");
                // OrderRequst.Instance.OrderPrint(2, 1);
            }
            catch (Exception)
            {
            }
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                PrinterWebSocket.Instance.DyCS1(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"打印\打印图片.jpg");
            }
            catch (Exception)
            {
            }
        } 
           //CoffeeMakeModel makeModel = new CoffeeMakeModel();
           //makeModel.Guid = guidnew;
           //makeModel.OrderMaking = OrderNumID;
           //makeModel.OrderGoodsId = list.id;
           //makeModel.OrderId = list.orderId;
           //makeModel.OrderNum = orderNum;
           //makeModel.Type = 0;
           //makeModel2s.Add(makeModel);
    }
}
